package com.sunday.aomicaichang.adpter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunday.aomicaichang.R;
import com.sunday.aomicaichang.ui.MenuDetailActivity;
import com.sunday.member.entity.product.ProductDetail;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yun_sheng on 2017/6/8.
 */

public class ProductMenuAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<ProductDetail.CookBooksBean> cookBooks;

    public ProductMenuAdapter(Context mContext) {
        this.mContext = mContext;

    }

    public void setmData(List<ProductDetail.CookBooksBean> cookBooks) {
        this.cookBooks = cookBooks;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_goods_menu, parent, false);
        return new ItemViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final ProductDetail.CookBooksBean cookBook = cookBooks.get(position);
        ItemViewHolder viewHolder = (ItemViewHolder) holder;
        if (cookBook.getImage() != null)
            Picasso.with(mContext)
                    .load(cookBook.getImage())
                    .error(R.mipmap.ic_product)
                    .into(viewHolder.img);
        if (cookBook.getName() != null)
            viewHolder.tvName.setText(cookBook.getName());
        if (cookBook.getViceName() != null)
            viewHolder.tvSub.setText(cookBook.getViceName());
        viewHolder.item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, MenuDetailActivity.class);
                intent.putExtra("id", cookBook.getId());
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return cookBooks == null ? 0 : cookBooks.size();
    }


    public class ItemViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.img)
        ImageView img;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_sub)
        TextView tvSub;
        @Bind(R.id.item)
        RelativeLayout item;

        public ItemViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}
